import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yp/config/config.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yp/pages/trolley/trolley_logic.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:get/get.dart';
import '../../../helper.dart';
import '../logic.dart';
import '../state.dart';
class SpecificationsWidget extends StatefulWidget {

  final Map<String,dynamic> data;
  final Map<String,dynamic> data1;
  final Map<String,dynamic> data2;
  int index;
  List data1List;
  final TrolleyLogic logic;
  SpecificationsWidget({Key key,this.data,this.data1,this.data2,this.data1List,this.index,this.logic}) : super(key: key);

  @override
  _SpecificationsWidgetState createState() => new _SpecificationsWidgetState();
}

class _SpecificationsWidgetState extends State<SpecificationsWidget>  {
  @override
  Widget build(BuildContext context) {
    return Container(
      child:GetBuilder<TrolleyLogic>(
        builder: (logic) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(widget.data['specs'][widget.index]["spec_name"],style: TextStyle(color: Color(0xff333333),fontSize: 28.sp),),
              SizedBox(height: 20.w,),
              Container(
                child: Wrap(
                  alignment: WrapAlignment.start,
                  runAlignment: WrapAlignment.end,
                  children: widget.data1List.map<Widget>((item) {
                    var keyId = item["spec_value_id"].toString();
                    bool isSelected = logic.state.selectSpecifications.contains(keyId);
                    return GestureDetector(
                      onTap: () {

                        List<String> list = logic.state.selectSpecifications.split('_');
                        list[widget.index] = keyId;

                        //修改选中项
                        logic.state.selectSpecifications = list.join('_').toString();
                         //修改item_num_id
                        logic.state.item_num_id = logic.state.goodsSpecificationsData["spec_item"][logic.state.selectSpecifications]["item_num_id"].toString();
                        //选中商品规格图片
                        logic.state.goodsSpecificationsImageData = logic.state.goodsSpecificationsData["spec_item"][logic.state.selectSpecifications]["image_default_id"];
                        //选中商品规格标题
                        logic.state.goodsSpecificationsTitleData = logic.state.goodsSpecificationsData["spec_item"][logic.state.selectSpecifications]["item_name"];
                        //选中商品规格价格
                        logic.state.sumParic = logic.state.goodsSpecificationsData["spec_item"][logic.state.selectSpecifications]["price"];
                        logic.update();
                         print(logic.state.goodsSpecificationsData["spec_item"][logic.state.selectSpecifications]["item_num_id"].toString());
                      },
                      child: Container(
                        padding:EdgeInsets.symmetric(horizontal: 22.w, vertical: 15.w),
                        margin: EdgeInsets.only(right: 20.w,top: 20.w,),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(40.w),
                          border: isSelected ?
                          Border.all(
                            color: Config.mainColor,
                            width: 2.w,
                            style: BorderStyle.solid,
                          )
                              :
                          Border.all(
                            color: Color(0xffEEEEEE),
                            width: 2.w,
                            style: BorderStyle.solid,
                          ),
                        ),
                        child: Text(
                          item["spec_value"],
                          style: TextStyle(color:isSelected ? Config.mainColor : Color(0xff222222), fontSize: 26.w),
                        ),
                      )
                    );
                  }).toList(),
                ),
              ),
            ],
          );
        }
      )
    );
  }
}